﻿using System.Data;
using Orchard.ContentManagement.MetaData;
using Orchard.Core.Contents.Extensions;
using Orchard.Data.Migration;
using Orchard.Indexing;
using EDB.SmoothStreaming.Models;

namespace EDB.SmoothStreaming {
    public class Migrations : DataMigrationImpl {

        public int Create() {
            SchemaBuilder.CreateTable("VideoPartRecord", table => table
                .ContentPartRecord()
                .Column("VideoId", DbType.Guid)
                .Column("ThumbnailWidth", DbType.Int32)
                .Column("ThumbnailHeight", DbType.Int32)
                .Column("ThumbnailCount", DbType.Int32)
                .Column("VideoStreams", DbType.String)
                .Column("PlayerWidth", DbType.Int32)
                .Column("PlayerHeight", DbType.Int32)
            );

            ContentDefinitionManager.AlterTypeDefinition("Video", cfg => cfg
              .WithPart("CommonPart")
              .WithPart("RoutePart")
              .WithPart("BodyPart")
              .WithPart("VideoPart")
              .WithPart("CommentsPart")
              .WithPart("TagsPart")
              .WithPart("LocalizationPart")
              .WithPart("ContainablePart")
              .Creatable()
              .Indexed());

            return 1;
        }

        public int UpdateFrom1()
        {
            ContentDefinitionManager.AlterPartDefinition(typeof(VideoPart).Name, cfg => cfg.Attachable());

            SchemaBuilder.AlterTable("VideoPartRecord", table => table.AddColumn("Duration", DbType.Int64));
            SchemaBuilder.AlterTable("VideoPartRecord", table => table.AddColumn("ThumbnailFormat", DbType.Int16));

            return 2;
        }
    }
}